package com.opgea.crm.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.domain.entities.Issue;
import com.opgea.crm.domain.entities.IssueComment;
import com.opgea.crm.domain.entities.ProjectDetails;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.entities.security.CrmUser;

@Repository
public class IssueRepoImpl implements IssueRepo{

	@PersistenceContext
	private EntityManager em;
	
	public Long create(Issue issue) {
		em.persist(issue);
		return issue.getId();
	}
    
	public void remove(Long id) {
		
	}
	@Transactional(propagation=Propagation.REQUIRED)
	
	public void update(Issue issue){
		em.merge(issue);		
	}
	
	public Issue find(Long id) {
		return em.find(Issue.class,id);
	}
    
	public List<Issue> findAll() {
		String query = "SELECT i FROM Issue i";
		return em.createQuery(query,Issue.class).getResultList();
	}
	
	@Override
	public List<Issue> findAllByProjectId(long projectId){
		String query = "SELECT p FROM Issue p WHERE p.project.id = " +projectId ;
		List<Issue> issueList =  em.createQuery(query,Issue.class).getResultList();
		return issueList;
		
	}
   @Override
	public List<IssueComment> findAllCommentsByIssueId(long issueId){ 
		String query = "SELECT c FROM IssueComment c where c.issue.id =" + issueId+" order by c.createdAt desc"; 
		List<IssueComment> issueList =  em.createQuery(query,IssueComment.class).getResultList();
		return issueList;
   }
	
  @Override
	public List<Issue> issueByProjectId(Long projectId,Long priorityId,Long assigntoId,CrmUser user) 
	{ 
		StringBuffer buffer=new StringBuffer("SELECT i FROM Issue i"); 
		String cond=" WHERE ";
		if(projectId != null)
		{
			buffer.append(cond);
			buffer.append(" i.project.id="+projectId);
			cond = " AND ";
		}
		if(priorityId != null)
		{ 
			buffer.append(cond);
			buffer.append(" i.priority.id="+priorityId);
			cond = " AND ";
		} 
		if(assigntoId!= null)
		{
			buffer.append(cond);
			buffer.append(" i.assignedTo.id="+assigntoId);
			buffer.append(" group by i.assignedTo.id =");  
			cond = " AND ";
		} 
		if(user!=null)
		{
			buffer.append(cond);
			buffer.append(" i.assignedTo.id="+user.getEmployee().getId());
		}
		
		Query query=em.createQuery(buffer.toString(),Issue.class);
		List<Issue> issues=query.getResultList();
		return issues;
	}
  

	 public List<ProjectDetails> allProjectAndMembers(Long projectId){
		 StringBuffer buffer=new StringBuffer("SELECT entObj FROM ProjectDetails entObj ");
			if(projectId!=null)
			{
				buffer.append(" WHERE entObj.id="+projectId);
			}
			Query query=em.createQuery(buffer.toString(),ProjectDetails.class);
			List<ProjectDetails> list=query.getResultList();
			return list;
		 }
	 
	 public List<SetupsValue> allProjects(){
		 
		 Query query=em.createQuery("SELECT new com.crm.domain.SetupsValue(entObj.id,entObj.projectName) FROM ProjectDetails entObj");
			List<SetupsValue> list=query.getResultList();
			return list;
	 }
	 public Issue findIssueById(Long issueId)
	 {
		 if(issueId!=null)
			 return em.find(Issue.class,issueId);
		 return null;
	 }
	
	
	

}
